﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CooStationsToolsNew.Model;
using System.Xml;
using System.IO;

namespace CooStationsToolsNew
{
    class PageTools:IDisposable  
    {
        public PageTools()
        {
        }
        public void Dispose()
        {
             
        }  

        ProjectTools pt = new ProjectTools();

        /// <summary>
        /// 增加一个页面
        /// </summary>
        /// <param name="cp"></param>
        /// <returns></returns>
        public bool AddPage(CfyPage cp)
        {
            try
            {
                pt.CreatCfyXml(App.appPath + "\\temp\\"+cp.PageName +".xml",
                    App.appPath + "\\temp\\"+cp.PageName +"_event.xml");
                App.Pagelist.Add(cp);
                SetPageName(cp.PageName);
                SetPage(cp);
                App.pageName = cp.PageName;
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="cp"></param>
        /// <returns></returns>
        public bool UpdatePage(CfyPage cp)
        {
            try
            {
                UpdatePageName(cp);
                SetPage(cp);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 修改页面名称
        /// </summary>
        /// <param name="p"></param>
        private void UpdatePageName(CfyPage cp)
        {
            int n = App.Pagelist.IndexOf(cp);
            XmlDocument Mydoc = new XmlDocument();
            Mydoc.Load(App.appPath + "\\temp\\" + App.proName + ".cfy");
            XmlNodeList list = Mydoc.ChildNodes[1].ChildNodes[6].ChildNodes;
            for (int i = 0; i < list.Count; i++)
            {
                if (i == n)
                {
                    XmlElement xe = (XmlElement)list[i];
                    xe.InnerXml = cp.PageName;
                }
            }
            Mydoc.Save(App.appPath + "\\temp\\" + App.proName + ".cfy");
        }

        /// <summary>
        /// 设置页面宽高
        /// </summary>
        /// <param name="cp"></param>
        public void SetPage(CfyPage cp)
        {
            try
            {
                XmlDocument Mydoc = new XmlDocument();
                Mydoc.Load(App.appPath + "\\temp\\" + cp.PageName + ".xml");

                XmlElement xe = (XmlElement)Mydoc.ChildNodes[1];
                xe.SetAttribute("width", cp.Width);
                xe.SetAttribute("height", cp.Height);
                if (cp.OnloadInit != null && cp.OnloadInit != "")
                {
                    xe.SetAttribute("onloadInit", cp.OnloadInit);
                }
                Mydoc.Save(App.appPath + "\\temp\\" + cp.PageName + ".xml");
            }
            catch { 
            
            }
        }

        /// <summary>
        /// 删除onload
        /// </summary>
        /// <param name="PageName"></param>
        public void DeletePageInit(string PageName)
        {
            XmlDocument Mydoc = new XmlDocument();
            Mydoc.Load(App.appPath + "\\temp\\" + PageName + ".xml");

            XmlElement xe = (XmlElement)Mydoc.ChildNodes[1];

            xe.RemoveAttribute("onloadInit");

            Mydoc.Save(App.appPath + "\\temp\\" + PageName + ".xml");
        }
        /// <summary>
        /// 增加到程序配置文件
        /// </summary>
        /// <param name="pageName"></param>
        public void SetPageName(string pageName)
        {
            XmlDocument Mydoc = new XmlDocument();
            Mydoc.Load(App.appPath + "\\temp\\" + App.proName + ".cfy");
            XmlNode list = Mydoc.ChildNodes[1].ChildNodes[6];

            XmlElement xe = Mydoc.CreateElement("name");
            xe.InnerXml = pageName;
            list.AppendChild(xe);

            Mydoc.Save(App.appPath + "\\temp\\" + App.proName + ".cfy");
        }

        /// <summary>
        /// 打开页面
        /// </summary>
        public void OpenPage()
        {
            ClearProList();

            App.dt = pt.OpenCfyXml(App.appPath + "\\temp\\" + App.pageName + ".xml");
            App.eventdt = pt.OpenCfyeventXml(App.appPath + "\\temp\\" + App.pageName + "_event.xml");
            App.ActionList = pt.OpenActionXml(App.appPath + "\\temp\\" + App.pageName + "_event.xml");
            App.scriptdt = pt.OpenScritpDt(App.appPath + "\\temp\\" + App.pageName + "_event.xml");
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearProList()
        {
            App.dt.Clear();
            App.eventdt.Clear();
            App.piclist.Clear();
            App.rectlist.Clear();
            App.ActionList.Clear();
            App.divlist.Clear();
            App.textlist.Clear();
            App.scriptdt.Clear();
        }
        /// <summary>
        /// 删除页
        /// </summary>
        /// <param name="pageName"></param>
        /// <returns></returns>
        public bool DeletePage(string pageName)
        {
            if (pageName.Equals(App.pageName))
            {
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    if (cp.PageName == pageName)
                    {
                        App.Pagelist.RemoveAt(i);
                        break;
                    }
                }
                try
                {
                    File.Delete(App.appPath + "\\temp\\" + App.pageName + ".xml");
                    File.Delete(App.appPath + "\\temp\\" + App.pageName + "_event.xml");
                    App.pageName = "cfy";
                    OpenPage();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    if (cp.PageName == pageName)
                    {
                        App.Pagelist.RemoveAt(i);
                        break;
                    }
                }
                try
                {
                    File.Delete(App.appPath + "\\temp\\" + pageName + ".xml");
                    File.Delete(App.appPath + "\\temp\\" + pageName + "_event.xml");
                    return true;
                }
                catch
                {
                    return false;
                }
            }

        }

    }
}
